package main.bytedance.huadongchuangkou;

/**
 *
 * @author kejl
 * @version test
 * @date 2020/5/26 22:38
 */
public class LengthOfLongestSubstring2 {


    public static void main(String[] args) {
        lengthOfLongestSubstring2("aaaaa");
    }

    /**
     * 滑动窗口的解法（其实就是正常的思维解法）
     * @param s
     * @return
     */
    public static  int lengthOfLongestSubstring2(String s){
        char[] chars = s.toCharArray();
        if(chars.length<=1){
            return chars.length;
        }
        int result = 0;
        String substr = "";
        for (int i = 0; i <chars.length ; i++) {
            String temp = chars[i]+"";
            if(substr.contains(temp)){
                //在子串中如果碰到相同的，则按照之前的子串，去掉前面匹配到的，然
                // 后加上这个继续往后增加字符串
                int distance = substr.length();
                result = result>distance?result:distance;
                //替换成新的字符串继续添加
                substr =substr.substring(substr.indexOf(temp)+1,substr.length());
            }
            substr = substr+temp;
        }
            System.out.println(result);
        return result>substr.length()?result:substr.length();
    }
}
